package com.example.mapper;

import org.apache.ibatis.annotations.Mapper;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Mapper
public interface Logging {
    File file = new File("src/main/resources/static/logging");
    List<String> lines = new ArrayList<>();

    public static void addLogging(String Info) throws IOException {
        lines.clear();
        getLogging();
        if (lines.size()>0&&lines.get(lines.size()-1).equals(Info))return;
        if (lines.size()==0)getLogging();
        lines.add(Info);
        if (lines.size() > 200) {
            lines.subList(0, 50).clear();
        }
        FileWriter writer = new FileWriter(file);
        BufferedWriter bufferedWriter = new BufferedWriter(writer);
        for (String line : lines) {
            bufferedWriter.write(line);
            bufferedWriter.newLine();
        }
        bufferedWriter.close();
        writer.close();
    }

    public static void deleteLogging() throws IOException {
        lines.clear();
        FileWriter writer = new FileWriter(file, false);
        BufferedWriter bufferedWriter = new BufferedWriter(writer);
        bufferedWriter.write("");
        bufferedWriter.close();
        writer.close();
    }

    public static List<String> getLogging() throws IOException {
        lines.clear();
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String line;
        while ((line = reader.readLine()) != null) {
            lines.add(line);
        }
        reader.close();
        List<String>tlist=new ArrayList<>();
        tlist.addAll(lines.stream().collect(Collectors.toList()));
        Collections.reverse(tlist);
        return tlist;
    }


}
